#include<stdio.h>
int main(void)
{
	int n,year,month,day,i,rank=0,k,sum=0;
	int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		sum = 0;
		rank = 0;
		scanf("%d%d%d",&year,&month,&day);
		if(year%4 == 0)
		{
			for(k=0;k<month-1;k++)
			sum += b[k];
		}
		else
		{	
			for(k=0;k<month-1;k++)
			sum += a[k];
		}
		rank = sum+day;
 		printf("%d\n",rank);
	}
	return 0;
}


